home *** CD-ROM | disk | FTP | other *** search
- Path: news.infi.net!usenet
- From: nngis@norfolk.infi.net (Greg DiGiorgio)
- Newsgroups: comp.lang.c
- Subject: Re: New C Programmer Has A Problem
- Date: 29 Jan 1996 13:28:15 GMT
- Organization: Customer of InfiNet
- Message-ID: <4eii1f$5il@nw002.infi.net>
- References: <4ehpa3$6kl@nntp.novia.net>
- Reply-To: nngis@norfolk.infi.net
- NNTP-Posting-Host: h-skeletoncrew.norfolk.infi.net
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.99.3
-
- In article <4ehpa3$6kl@nntp.novia.net>, tsyslo@oasis.novia.net says...
- >
- >
- > START MESSAGE:
- >
- >
- > I have a question on C... why isn't this program working?!
- >
- > Program START:
- > /* File Open */
- >
- > #include <clib/dos_protos.h>
- > #include <dos/dos.h>
- > #include <stdio.h>
- > #include <exec/types.h>
- >
- > void main();
- >
- > char *name;
- > int age;
- >
- > void main()
-
- *** First watch out for the ANSI/ISO police on typing "main" to not
- *** return a value. You might try "int main()".
-
- >
- > {
- > struct FileHandle *file_handle;
- > long bytes_written;
- > long bytes_read;
- >
- > printf("Enter your name: ");
- > scanf("%s",name);
-
- *** Second, you are reading input into an undefined area of memory
- *** in that you have not set "*name" to point to anything. You could
- *** be overwriting anything at this point. Very dangerous! You should
- *** be using a buffer, not an undefined pointer. For example,
- *** char name[80];
-
- > printf("\nEnter your age: "); scanf("%d",age);
-
- *** Third, (you're new to 'C', aren't you?) you must precede variables
- *** in a "scanf" with the "&" operator to tell "scanf" where to put
- *** the input value. This does not,however, apply to arrays and pointers,
- *** but you're using an "int" so you need to do it. That is, recode
- *** "scanf("%d",age);" as "scanf("%d",&age);". The way you have coded it
- *** you are again overwriting memory somewhere, but you are not putting
- *** a value in "age" (more than likely). You are putting a value in the
- *** memory address that corresponds to the value integer value contained
- *** in "age", not in "age" itself.
-
- *** My eyes are getting tired, so I'll leave the rest for somebody else.
- *** Believe me, there is enough here to keep you busy for awhile.
-
- > ... Snipped ...
-
- Hope this helps,
- Greg DiGiorgiop
-
-